<?php
/**
 * 城市提升-地方车展-车展场租费用
 * @author jianhui
 *
 */
class CarRent extends MY_Controller{
    function CarRent(){
        parent::MY_Controller();
    }
    /**
     * 添加或修改车展场租费用
     */
    function add(){
        $this->load->model('carRentModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'   => $this->user['userId'],
                'procId'    => (int)$this->input->post('procId'),
                'projectId' => (int)$this->input->post('projectId'),
                'unitPrice' => (float)$this->input->post('unitPrice'),//场租单价
                'areaSize'  => (float)$this->input->post('areaSize'),//展位面积
                'storeNum'  => (int)$this->input->post('storeNum'),//参展专营店数量
                'budgetQiTotalType' => (int)$this->input->post('budgetQiTotalType'),
                'budgetQiTotal' => (float)$this->input->post('budgetQiTotal'),
                'isSync'        => 0,
            );
            $data['procId'] || showError('请选择主线流程');
            $data['projectId'] || showError('请选择车展');
            $data['unitPrice'] || showError('请输入场租单价');
            $data['areaSize'] || showError('请输入场租面积');
            $data['storeNum'] || showError('请输入参展专营店数量');
            $data['budgetQiTotalType'] || showError('请选择启辰店出资总额');
            if ($data['budgetQiTotalType'] == -1 && $data['budgetQiTotal'] < 0){
                showError('请输入启辰店出资总额自定义值');
            }
            if ($data['budgetQiTotalType'] != -1){
                $qiTotalType = $this->config->item('carshowQiTotalType');
                $data['budgetQiTotal'] = isset($qiTotalType[$data['budgetQiTotalType']]) ? $qiTotalType[$data['budgetQiTotalType']] : 0;
            }
            if ($id){
                $id = $this->carRentModel->edit($id, $data);
            }else{
                if ($this->carRentModel->getData(array('storeId'=>$data['storeId'], 'procId'=>$data['procId'],'projectId'=>$data['projectId']))){
                    showError('场租只允许添加一个');
                }
                $data['createTime'] = time();
                $id = $this->carRentModel->add($data);
            }
            if ($id){
                //更新费用
                $this->carRentModel->updateMoney($id);
                showSuccess('保存车展场租成功');
            }else{
                showError('保存车展场租失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $rentData = $this->carRentModel->getData(array('id'=>$id,'storeId'=>$this->user['userId']));
                $rentData || showError('场租数据不存在');
                in_array($rentData['budgetAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
                $this->viewData['rentData'] = $rentData;
                $procId = $rentData['procId'];
                $projectId = $rentData['projectId'];
            }else{
                $procId = (int)$this->input->get('procId');
                $procId || showError('请选择主线流程');
                $projectId = (int)$this->input->get('projectId');
                $projectId || showError('请选择车展ID');
            }
            $this->viewData['procId'] = $procId;
            $this->viewData['projectId'] = $projectId;
            //主线流程数据
            $this->load->model('carProcessModel');
            $procData = $this->carProcessModel->getData($this->viewData['procId']);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            //是否为特殊补报
            $this->viewData['isSpecialBubao'] = $this->auth->isCarBudgetEdit($procData);
            //车展数据
            $this->load->model('carProjectModel');
            $carshowData = $this->carProjectModel->getData($this->viewData['projectId']);
            $carshowData || showError('车展数据不存在');
            $this->viewData['carshowData'] = $carshowData;
            
            $this->load->view('CarRent/add', $this->viewData);
        }
    }
    /**
     * 删除车展场租费用
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的车展场租');
        $this->load->model('carRentModel');
        if ($this->carRentModel->del(array('id'=>$id, 'storeId'=>$this->user['userId']) )){
            showSuccess('删除车展场租成功');
        }else{
            showError('删除车展场租失败');
        }
    }
}